package com.sdgakj.web.order;

import com.google.common.collect.Maps;
import com.sdgakj.entity.Donation;
import com.sdgakj.entity.Order;
import com.sdgakj.enums.EnumOrderStatus;
import com.sdgakj.service.account.AccountService;
import com.sdgakj.service.order.OrderService;
import com.sdgakj.service.orderFlow.DonationService;
import com.sdgakj.utils.LoginAdminUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springside.modules.web.Servlets;

import javax.servlet.ServletRequest;
import java.util.Map;


@Controller
@RequestMapping(value = "/order")
public class OrderController
{
    
    private static final String PAGE_SIZE = "10";

    @Autowired
    AccountService accountService;
    @Autowired
    OrderService orderService;
    @Autowired
    DonationService donationService;

    private static final Logger logger = Logger.getLogger(OrderController.class);

    private static Map<String, String> sortTypes = Maps.newLinkedHashMap();
    
    static
    {
        sortTypes.put("auto", "自动");
    }

    private String getOrderStatus(int state) {
        switch (state) {
            case 0:
                return "已取消";
            case 1:
                return "等待付款";
            case 2:
                return "付款完成";
            case 3:
                return "交易完成";
            case 4:
                return "订单取消";
            case 5:
                return "退款审核中";
            case 6:
                return "退款中";
            case 7:
                return "退款完成";
            case 8:
                return "已打款";
            case 9:
                return "退款拒绝";
            case 10:
                return "打款失败";
            case 11:
                return "打款失败";
        }
        return "";
    }

    /**
     * 确认订单退款 页面跳转
     * 
     * @param
     * @return
     */
    @RequestMapping(value = "checkRefund/{id}", method = RequestMethod.GET)
    public String checkRefund(@PathVariable("id") Long id,
            @RequestParam(value = "type", defaultValue = "") Integer type, Model model) {
        Order order = orderService.getOrderById(id);
        model.addAttribute("order", order);

        if (type == 2) {
            return "order/refundDetectOrder";
        } else {
            return "order/refundMealOrder";
        }
    }

    /**
     * 列表
     * @param pageNumber 页号
     * @param pageSize   每页大小
     * @param sortType   排序字段
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value="/list",method = RequestMethod.GET)
    public String list(@RequestParam(value = "page", defaultValue = "1") int pageNumber,
            @RequestParam(value = "page.size", defaultValue = PAGE_SIZE) int pageSize,
            @RequestParam(value = "sortType", defaultValue = "auto") String sortType, Model model,
            ServletRequest request) {
        Map<String,Object> searchParams = Servlets.getParametersStartingWith(request, "search_");
       // Page<Order> orders = orderService.getOrderByPage(searchParams, pageNumber, pageSize, "createDate", sortType);
        Page<Donation> donations= donationService.getOrderByPages(searchParams, pageNumber, pageSize, "createtime", sortType);
       // model.addAttribute("orders",orders);
       // model.addAttribute("sortType", sortType);
        model.addAttribute("donations",donations);
        //model.addAttribute("searchParams", Servlets.encodeParameterStringWithPrefix(searchParams, "search_"));
        return "order/donationList";
    }

    /**
     * 确认订单退款 页面跳转
     *
     * @param
     * @return
     */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public String createDevote(@RequestParam(value = "id", defaultValue = "0") Long id,
            @RequestParam(value = "contactName", defaultValue = "0") String contactName,
            @RequestParam(value = "title",defaultValue = "0")String title,
            @RequestParam(value = "oDesp",defaultValue = "0")String oDesp,
            @RequestParam(value = "authTel", defaultValue = "0") String authTel,
            @RequestParam(value = "targetNum", defaultValue = "0") Integer targetNum,
            @RequestParam(value = "realNum", defaultValue = "0") Integer realNum,
            @RequestParam(value = "amount", defaultValue = "0") Double amount, RedirectAttributes redirectAttributes) {
        Order order = orderService.getOrderById(id);
        order.setTitle(title);
        order.setoDesp(oDesp);
        order.setAuthTel(authTel);
        order.setRealNum(realNum);
        order.setTargetNum(targetNum);
        order.setAmount(amount);
        order.setUserId(LoginAdminUtil.getCurrentUserId());
        orderService.saveOrder(order);
        redirectAttributes.addFlashAttribute("message", "更新成功");
        return "redirect:/order/list";
    }

    /**
     * 创建订单页面跳转
     *
     * @param
     * @return
     */
    @RequestMapping(value = "create", method = RequestMethod.GET)
    public String createDevote( Model model) {
        model.addAttribute("action",  "create");
        return "order/orderForm";
    }

    /**
     * 创建订单页面跳转
     *
     * @param
     * @return
     */
    @RequestMapping(value = "create", method = RequestMethod.POST)
    public String createDevote(@RequestParam(value = "targetNum", defaultValue = "0") Integer targetNum,
                               @RequestParam(value = "title") String title,
                               @RequestParam(value = "oDesp") String oDesp,
                               @RequestParam(value = "realNum", defaultValue = "0") Integer realNum,
                               @RequestParam(value = "authTel", defaultValue = "0") String authTel,
                               @RequestParam(value = "amount", defaultValue = "0") Double totalPrice, RedirectAttributes redirectAttributes) {
        Donation donation = new Donation();
        donation.setState(EnumOrderStatus.NEEDPAY.getCode());
        donation.setTitle(title);
        donation.setoDesp(oDesp);
        donation.setAuthTel(authTel);
        donation.setRealNum(realNum);
        donation.setTargetNum(targetNum);
        donation.setAmount(totalPrice);
        donation.setUserId(LoginAdminUtil.getCurrentUserId());
        donationService.saveDonation(donation);
        redirectAttributes.addFlashAttribute("message", "更新成功");
        return "redirect:/order/list";
    }

    /**
     * 更新订单页面跳转
     *
     * @param
     * @return
     */
    @RequestMapping(value = "update", method = RequestMethod.GET)
    public String updateDevote(@RequestParam(value = "id", defaultValue = "0") Long id, Model model) {
        Order order = orderService.getOrderById(id);
        model.addAttribute("order", order);
        model.addAttribute("action",  "update");
        return "order/orderForm";
    }
}
